package com.longrise.co.ui.carlife.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.longrise.co.R;
import com.longrise.co.api.ApiConstants;
import com.longrise.co.ui.carfiles.contract.CarFilesContract;
import com.longrise.co.ui.carfiles.model.CarFilesModel;
import com.longrise.co.ui.carfiles.presenter.CarFilesPresenter;
import com.longrise.co.ui.carlife.adapter.SelectCarAdapter;
import com.longrise.co.ui.home.model.BindCarInfo;
import com.longrise.co.utils.Constants;
import com.longrise.co.utils.StringUtils;
import com.ly.common.base.BaseFragment;
import com.ly.common.commonutils.TimeUtil;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by luoyang on 2017-7-22.
 */

public class  SelectCarFragment extends BaseFragment<CarFilesPresenter, CarFilesModel> implements CarFilesContract.View<BindCarInfo>, SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.OnRecyclerViewItemClickListener {


    @Bind(R.id.head_title)
    TextView headTitle;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.refresh_layout)
    SwipeRefreshLayout refreshLayout;
    @Bind(R.id.search_no_data_tv)
    TextView searchNoDataTv;
    @Bind(R.id.no_data_tip)
    RelativeLayout noDataTip;

    private SelectCarAdapter mAdapter;
    //违章查询
    public static final String FROM_ILLEGAL_INQUIRIES = "1";
    //年检查询
    public static final String FROM_ANNOUNCEMENT = "2";

    private String mCurrentType;

    @Override
    protected int getLayoutResource() {
        return R.layout.fg_select_car;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    protected void initView() {
        setText(headTitle, "请选择需要查询的车辆");
        mCurrentType = getArguments().getString("type");
        searchNoDataTv.setText("未查询到绑定车辆");
        initRecyclerView();
        onRefresh();
    }


    @Override
    public void requestSuccess(BindCarInfo bindCarInfo) {
        if (bindCarInfo != null && bindCarInfo.getList() != null && bindCarInfo.getList().size() > 0) {
            mAdapter.setNewData(bindCarInfo.getList());
            mAdapter.notifyDataChangedAfterLoadMore(false);
        } else {
            //没有搜索到结果
            noDataTip.setVisibility(View.VISIBLE);
            mAdapter.notifyDataChangedAfterLoadMore(false);
        }
    }

    private void initRecyclerView() {
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setColorSchemeColors(Constants.RECYCLER_RGB);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView.setLayoutManager(layoutManager);
        mAdapter = new SelectCarAdapter(null);

        mAdapter.openLoadMore(ApiConstants.PAGE_SIZE_10, true);
        mAdapter.setOnRecyclerViewItemClickListener(this);
        recyclerView.setAdapter(mAdapter);
    }

    public static SelectCarFragment newInstance(String type) {

        Bundle args = new Bundle();
        args.putString("type", type);
        SelectCarFragment fragment = new SelectCarFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onComplete() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.setRefreshing(false);
        }
    }

    @OnClick(R.id.head_back)
    public void onViewClicked() {
        _mActivity.onBackPressed();
    }

    @Override
    public void onRefresh() {
        noDataTip.setVisibility(View.GONE);
        mPresenter.getBindCarList(null, true);
    }

    @Override
    public void onItemClick(View view, int i) {
        BindCarInfo.ListBean carInfo = mAdapter.getItem(i);
        if(mCurrentType == FROM_ILLEGAL_INQUIRIES) {
            //违章查询
            start(IllegalInquiriesFragment.newInstance(carInfo.getCarno(), carInfo.getVin(), carInfo.getEnginenumber()));
        } else if(mCurrentType == FROM_ANNOUNCEMENT) {
            //年检提醒
            if(StringUtils.isEmpty(carInfo.getRegisterdate())) {
                showLongToast("当前车辆未上传注册日期");
                return;
            }
            start(YearCheckFragment.newInstance(carInfo.getCarno(), TimeUtil.getDateYMD(carInfo.getRegisterdate()),carInfo.getCartypecode()));
        }
    }

}
